$(function () {
    //票据类型
    $("#form_fksh_pj").find("[idVal='fksh_pjlx_id']").sjzdcombobox("pjlx");

    //申报公司对申报部门申报人的级联操作
    $("#fksh_sbgs_id").jgbmtree($("#fksh_sbbm_id"),$("#fksh_sbr_id"),$("#fksh_sbbm_id").val(),$("#fksh_sbr_id").val());

    //
    //删除电子借款单的按钮触发
    $("#btn_pj_del").click(function () {
        var pjRow = $('#fksh_pj_grid').edatagrid('getSelected');
        var pjIndex = $('#fksh_pj_grid').edatagrid('getRowIndex',pjRow);
        if(pjRow){
            $('#fksh_pj_grid').edatagrid('deleteRow',pjIndex);
            countSumPj();
        } else {
            $.messager.alert('提示','请选择要删除的票据!');
        }
    });

    var gsid = $("#skf_gs_id").val();
    $("#fksh_pj_grid").edatagrid({
        url:$.webPath() + "/zjgl/pjgl/pjglList?gsId="+gsid+"&&pjsflxType=3",
        toolbar:"#pj_tool",
        fitColumns: true,
        singleSelect: true,
        checkOnSelect: false,
        pageNumber: 1,
        selectOnCheck: false,
        rownumbers: true,
        showFooter:true,
        columns: [
            [
                {field:'id', title:'ID', hidden:true},
                {field:'pjhm', title:'票据号码', width:20,align: 'center'},
                {field:'dqje', title:'票据金额', width:20, formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                } ,align: 'right'
                },
                {field:'dqr', title:'到期日', width:20,align: 'center'},
                {field:'bz', title:'备注', width:20,align: 'center'}
            ]
        ]
    })

    //搜索票据
    $("#btn_pj_search").click(function () {
        searcPjhwindow(fksh_pj_searchWindowOKbtnHandler);
    });
});


//查询销售单确定按钮回调函数
function fksh_pj_searchWindowOKbtnHandler(datas) {
    $.each(datas, function (i,value) {
        $("#fksh_pj_grid").datagrid('appendRow', {
            'id': value.id,
            'pjhm': value.pjhm,
            'dqje': value.dqje.toString(),
            'dqr': value.dqr.toString(),
            'bz': value.bz
        });
        countSumPj();
    });
}

//计算计划总量
function countSumPj() {
    var sumdqje = 0;
    var data = $("#fksh_pj_grid").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var dqje=parseFloat(data.rows[i]['dqje']);
        dqje = isNaN(dqje) ? 0 : dqje;
        sumdqje += dqje;
    }
    addFooter(sumdqje);
}

function addFooter(sumdqje) {
    var footer = null;
    footer = [{"spr_jc": "合计", "dqje": formatMoney(sumdqje)}];
    if (footer != null)
        $("#fksh_pj_grid").edatagrid('reloadFooter',footer);
}
